/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014-2015 Umeng, Inc
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.umeng.comm.ui.activities;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.TextView;

import com.umeng.comm.core.listeners.Listeners.OnResultListener;
import com.umeng.comm.core.utils.ResFinder;
import com.umeng.comm.ui.fragments.ActiveUserFragment;
import com.umeng.comm.ui.fragments.HotTopicFeedFragment;
import com.umeng.comm.ui.fragments.LatestTopicFeedFragment;
import com.umeng.comm.ui.fragments.RecommendTopicFeedFragment;
import com.umeng.comm.ui.fragments.TopicFeedFragment;
import com.umeng.comm.ui.imagepicker.activities.TopicDetailBaseActivity;
import com.umeng.comm.ui.imagepicker.util.ViewFinder;

/**
 * 话题详情页
 */
public class TopicDetailActivity extends TopicDetailBaseActivity {

    /**
     * 话题详情的Fragment
     */
    //最新
    private TopicFeedFragment mDetailFragment;
    //活跃用户
    private ActiveUserFragment mActiveUserFragment;
    //热门
    private HotTopicFeedFragment hotTopicFeedFragment;
    //推荐
    private RecommendTopicFeedFragment recommendTopicFeedFragment;
    //最后回复
    private LatestTopicFeedFragment lastestTopicFeedFragment;

    private TextView hotTV;
    private TextView lastTV;
    private TextView tuiTV;
    private View view1;
    private View view2;
    private View view3;


    Bundle bundle;


//    @Override
//    protected void initTitles() {
//        mTitles = getResources().getStringArray(
//                ResFinder.getResourceId(ResType.ARRAY, "umeng_comm_topic_detail_tabs"));
//    }



    @Override
    protected int getLayout() {
         return ResFinder.getLayout("umeng_comm_topic_detail_layout_new");
    }



    @Override
    public void initFragment() {

        // 初始化热门、最新、推荐的listener

        hotTV = (TextView)headerView.findViewById(ResFinder.getId("topic_hot_tv"));
        hotTV.setOnClickListener(this);
        lastTV = (TextView)headerView.findViewById(ResFinder.getId("topic_last_tv"));
        lastTV.setOnClickListener(this);
        tuiTV = (TextView)headerView.findViewById(ResFinder.getId("topic_tui_tv"));
        tuiTV.setOnClickListener(this);
        view1 = headerView.findViewById(ResFinder.getId("view1"));
        view2 = headerView.findViewById(ResFinder.getId("view2"));
        view3 = headerView.findViewById(ResFinder.getId("view3"));
        changeTextColor(true,false,false);
        // 视图查找器
        mViewFinder = new ViewFinder(getWindow().getDecorView());

        if (hotTopicFeedFragment == null) {
            hotTopicFeedFragment = HotTopicFeedFragment.newTopicFeedFrmg(mTopic);
            bundle = new Bundle();
            bundle.putBoolean("isHome", true);
            hotTopicFeedFragment.setArguments(bundle);
            hotTopicFeedFragment.setHeaderView(headerView);
        }
        hotTopicFeedFragment.setOnAnimationListener(mListener);
        addFragment(ResFinder.getId("umeng_topic_fragment_container"),
                hotTopicFeedFragment);
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        if (id == ResFinder.getId("topic_hot_tv")) {// 热门
//            if (mCurrentFragment instanceof HotTopicFeedFragment) { // 已经处于当前页面，判断是否需要滚动到起始位置
//
//            } else {
//                showFragment(hotTopicFeedFragment);
//            }

            transaction.remove(hotTopicFeedFragment);
            if (mFragmentManager.findFragmentByTag(hotTopicFeedFragment.getClass().getName()) == null) {
                transaction
                        .add(mFragmentContainer, hotTopicFeedFragment, hotTopicFeedFragment.getClass().getName());
            } else {
                hotTopicFeedFragment = HotTopicFeedFragment.newTopicFeedFrmg(mTopic);
                hotTopicFeedFragment.setHeaderView(headerView);
                bundle = new Bundle();
                bundle.putBoolean("isHome", true);
                hotTopicFeedFragment.setArguments(bundle);
                transaction
                        .add(mFragmentContainer, hotTopicFeedFragment, hotTopicFeedFragment.getClass().getName());
//                        transaction.show(mReleaseFragment);
            }
            transaction.commitAllowingStateLoss();
            mCurrentFragment = hotTopicFeedFragment;//重新进来后，点击切换分类会
            changeTextColor(true,false,false);
        } else if (id == ResFinder.getId("topic_last_tv")) {// 最新
            if (mDetailFragment == null) {
                mDetailFragment = TopicFeedFragment.newTopicFeedFrmg(mTopic);
                mDetailFragment.setHeaderView(headerView);
                showFragment(mDetailFragment);

            }else {
                transaction.remove(mDetailFragment);

                if (mFragmentManager.findFragmentByTag(mDetailFragment.getClass().getName()) == null) {
                    transaction
                            .add(mFragmentContainer, mDetailFragment, mDetailFragment.getClass().getName());
                } else {
                    mDetailFragment = TopicFeedFragment.newTopicFeedFrmg(mTopic);
                    mDetailFragment.setHeaderView(headerView);
                    transaction
                            .add(mFragmentContainer, mDetailFragment, mDetailFragment.getClass().getName());
                }
                transaction.commitAllowingStateLoss();
                mCurrentFragment = mDetailFragment;
            }
            bundle = new Bundle();
            bundle.putBoolean("isHome", true);
            mDetailFragment.setArguments(bundle);
            mDetailFragment.setOnAnimationListener(mListener);
//            if (mCurrentFragment instanceof TopicFeedFragment
//                    && !(mCurrentFragment instanceof RecommendTopicFeedFragment)) {
//            } else {
//                showFragment(mDetailFragment);
//            }
            changeTextColor(false,true,false);
        } else if (id == ResFinder.getId("topic_tui_tv")) { // 推荐
            if (recommendTopicFeedFragment == null) {
                recommendTopicFeedFragment = RecommendTopicFeedFragment.newTopicFeedFrmg(mTopic);
                recommendTopicFeedFragment.setHeaderView(headerView);
                showFragment(recommendTopicFeedFragment);
            }else {
                transaction.remove(recommendTopicFeedFragment);

                if (mFragmentManager.findFragmentByTag(recommendTopicFeedFragment.getClass().getName()) == null) {
                    transaction
                            .add(mFragmentContainer, recommendTopicFeedFragment, recommendTopicFeedFragment.getClass().getName());
                } else {
                    recommendTopicFeedFragment = RecommendTopicFeedFragment.newTopicFeedFrmg(mTopic);
                    recommendTopicFeedFragment.setHeaderView(headerView);
                    transaction
                            .add(mFragmentContainer, recommendTopicFeedFragment, recommendTopicFeedFragment.getClass().getName());
                }
                transaction.commitAllowingStateLoss();

                mCurrentFragment = recommendTopicFeedFragment;
            }
            bundle = new Bundle();
            bundle.putBoolean("isHome", true);
            recommendTopicFeedFragment.setArguments(bundle);
            recommendTopicFeedFragment.setOnAnimationListener(mListener);
//            if (mCurrentFragment instanceof RecommendTopicFeedFragment) {
//            } else {
//
//            }
            changeTextColor(false,false,true);
        } else if (id == ResFinder.getId("umeng_comm_title_back_btn")) { // 返回
            this.finish();
        }
    }

    private void changeTextColor(boolean hot, boolean last, boolean tui) {
        hotTV.setSelected(hot);
        lastTV.setSelected(last);
        tuiTV.setSelected(tui);
        if(hot){
            view1.setVisibility(View.VISIBLE);
            view2.setVisibility(View.GONE);
            view3.setVisibility(View.GONE);
        }else if(last){
            view1.setVisibility(View.GONE);
            view2.setVisibility(View.VISIBLE);
            view3.setVisibility(View.GONE);
        }else {
            view1.setVisibility(View.GONE);
            view2.setVisibility(View.GONE);
            view3.setVisibility(View.VISIBLE);
        }



    }


//    @Override
//    protected void gotoPostFeedActivity() {
//        Intent postIntent = new Intent(TopicDetailActivity.this, PostFeedActivity.class);
//        postIntent.putExtra(Constants.TAG_TOPIC, mTopic);
//        startActivity(postIntent);
//    }

//    /**
//     * 获取对应的Fragment。0：话题聚合 1：活跃用户</br>
//     *
//     * @param pos
//     * @return
//     */
//    protected Fragment getFragment(int pos) {
//        if (pos == 0) {
//            if (mDetailFragment == null) {
//                mDetailFragment = TopicFeedFragment.newTopicFeedFrmg(mTopic);
//            }
//            mDetailFragment.setOnAnimationListener(mListener);
//            return mDetailFragment;
//        } else if (pos == 1) {
//            if (lastestTopicFeedFragment == null) {
//                lastestTopicFeedFragment = LatestTopicFeedFragment.newTopicFeedFrmg(mTopic);
//            }
//            lastestTopicFeedFragment.setOnAnimationListener(mListener);
//            return lastestTopicFeedFragment;
//        }else if (pos == 2) {
//            if (recommendTopicFeedFragment == null) {
//                recommendTopicFeedFragment = RecommendTopicFeedFragment.newTopicFeedFrmg(mTopic);
//            }
//            recommendTopicFeedFragment.setOnAnimationListener(mListener);
//            return recommendTopicFeedFragment;
//        }else if (pos == 3) {
//            if (hotTopicFeedFragment == null) {
//                hotTopicFeedFragment = HotTopicFeedFragment.newTopicFeedFrmg(mTopic);
//            }
//            hotTopicFeedFragment.setOnAnimationListener(mListener);
//            return hotTopicFeedFragment;
//        }else if (pos == 4) {
//            if (mActiveUserFragment == null) {
//                mActiveUserFragment = ActiveUserFragment.newActiveUserFragment(mTopic);
//            }
//            mActiveUserFragment.setOnAnimationListener(mListener);
//            return mActiveUserFragment;
//        }
//        return null;
//    }

    private OnResultListener mListener = new OnResultListener() {

        @Override
        public void onResult(int status) {
//            if (status == 1) {// dismiss
//                mCustomAnimator.startDismissAnimation(mHeaderView);
//            } else if (status == 0) { // show
//                mCustomAnimator.startShowAnimation(mHeaderView);
//            }
        }
    };





}
